/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jili.ubert.test;

import com.alibaba.fastjson.JSON;
import com.jili.ubert.code.client2server.NewOrder;
import com.jili.ubert.dao.NewHibernateUtil;
import com.jili.ubert.dao.sim.TradeNeworder;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.sql.Date;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChengJili
 */
public class DaoCode {
    private static final Log log = LogFactory.getLog(DaoCode.class);
    public static void main(String[] args) throws UnknownHostException, SocketException, IOException {
     //   TradeNeworderCopy order = new TradeNeworderCopy();
        NewOrder order = new NewOrder();
        order.setAccountCode("26513SDD");
        order.setAlterUserId("jili");
        order.setBs("B");
        order.setClassCode("A1");
        order.setCloseType("!");
        order.setDateTime(Date.valueOf(LocalDate.now()));
        order.setDateTimeStr(LocalDateTime.now().toString());
        order.setDealerUserId("jili");
        order.setHedgeFlag("1");
        InetAddress addr = InetAddress.getLocalHost();
        order.setIp4(Ip2Int(addr.getHostAddress().toString()));
        order.setMac(getLocalMac(addr));
        order.setMarketCode("S");
        order.setObj("600000");
        order.setOpenClose("O");
        order.setOrderId(1);
        order.setOrderPrice((float)6.7);
        order.setOrderQty(100);
        order.setPosId("");
        order.setPriceType("1");
        order.setSubmitTime(Time.valueOf(LocalTime.now()));
        order.setTradeDate(Date.valueOf(LocalDate.now()));
        byte[] dd = order.EnCode(order);
        log.info(Arrays.toString(dd));
        log.info(JSON.toJSON(order));
        
        NewOrder order1 = new NewOrder();
        order1 = order1.DeCode(dd);
        order1.setOrderId(2);
        log.info(JSON.toJSON(order1));
        order1.getDateTime();
        
        NewOrder order2 = new NewOrder();
        order2 = order2.DeCode(dd);
        order2.setOrderId(2);
        log.info(JSON.toJSON(order2));
        
        
        
        Session session = NewHibernateUtil.getOpenSession();
        Transaction t = session.beginTransaction();
        order1.setDateTime(Date.valueOf(LocalDate.now()));
        order1.setSubmitTime(Time.valueOf(LocalTime.now()));
        order1.setTradeDate(Date.valueOf(LocalDate.now()));
        log.info(JSON.toJSON(order1));
        session.saveOrUpdate(order);
        session.saveOrUpdate(order1);
        log.info("事务提交开始");
        t.commit();
        log.info("事务提交完成");
        NewHibernateUtil.closeSession(session);
                
        
    }

    private static String getLocalMac(InetAddress ia) throws SocketException {
		// TODO Auto-generated method stub
        //获取网卡，获取地址
        byte[] mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
     //   System.out.println("mac数组长度：" + mac.length);
        StringBuffer sb = new StringBuffer("");
        for (int i = 0; i < mac.length; i++) {
            if (i != 0) {
                sb.append("-");
            }
            //字节转换为整数
            int temp = mac[i] & 0xff;
            String str = Integer.toHexString(temp);
        //    System.out.println("每8位:" + str);
            if (str.length() == 1) {
                sb.append("0" + str);
            } else {
                sb.append(str);
            }
        }
        return sb.toString().toUpperCase();
    }
    public static int Ip2Int(String strIp){
        String[] ss = strIp.split("\\.");
        if(ss.length != 4){
            return 0;
        }
        byte[] bytes = new byte[ss.length];
        for(int i = 0; i < bytes.length; i++){
            bytes[i] = (byte) Integer.parseInt(ss[i]);
        }
        return byte2Int(bytes);
    }
    public static String int2Ip(int intIp){
        byte[] bytes = int2byte(intIp);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < 4; i++){
            sb.append(bytes[i] & 0xFF);
            if(i < 3){
                sb.append(".");
            }
        }
        return sb.toString();
    }

    private static byte[] int2byte(int i) {
        byte[] bytes = new byte[4];
        bytes[0] = (byte) (0xff & i);
        bytes[1] = (byte) ((0xff00 & i) >> 8);
        bytes[2] = (byte) ((0xff0000 & i) >> 16);
        bytes[3] = (byte) ((0xff000000 & i) >> 24);
        return bytes;
    }
    private static int byte2Int(byte[] bytes) {
        int n = bytes[0] & 0xFF;
        n |= ((bytes[1] << 8) & 0xFF00);
        n |= ((bytes[2] << 16) & 0xFF0000);
        n |= ((bytes[3] << 24) & 0xFF000000);
        return n;
    }
}
